package day20;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.SequenceInputStream;
import java.util.Enumeration;
import java.util.Vector;


/**
 *
 * SequenceInputStreamDemo.java
 * @author wangbiao<br/>
 *         email:785497344@qq.com
 * @description 合并流，合并多个文件到一个文件中
 * @date 2013-6-26下午11:16:16
 *
 */
public class SequenceInputStreamDemo {

	public static void main(String[] args) {
		Vector<FileInputStream> vectors = new Vector<FileInputStream>();
		SequenceInputStream sis = null;
		FileOutputStream fos = null;
		try {
			vectors.add(new FileInputStream("d:\\1.txt"));
			vectors.add(new FileInputStream("d:\\2.txt"));
			vectors.add(new FileInputStream("d:\\3.txt"));
			Enumeration<FileInputStream> e = vectors.elements();
			sis = new SequenceInputStream(e);
			int len = 0;
			byte[] b = new byte[1024];	
			fos = new FileOutputStream("d:\\4.txt");
			while ((len = sis.read(b)) != -1) {
				fos.write(b, 0, len);
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (null != fos) {
					fos.close();
				}
				if (null != sis) {
					sis.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}
